package com.tzy.rabbitmq.publish_subscribe;

import com.rabbitmq.client.*;
import com.tzy.rabbitmq.constant.RabbitmqConstant;
import com.tzy.rabbitmq.utils.RabbitmqUtil;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
 * 腾讯新闻获取天气
 * @author 自律且自强ing
 */
public class Tencent {

    public static void main(String[] args) throws IOException, TimeoutException {
        //获取TCP长连接
        Connection connection = RabbitmqUtil.getConnection();

        //获取虚拟连接 | 创建信道
        final Channel channel = connection.createChannel();
        ///声明队列信息
        channel.queueDeclare();
        //queueBind用于将队列与交换机绑定
        //参数一:队列名; 参数二:交换机名; 参数三:路由Key (暂时用不到)
        channel.queueBind(RabbitmqConstant.QUEUE_TENCENT,RabbitmqConstant.EXCHANGE_WEATHER,"");
        //
        channel.basicQos(1);
        // consume:消费,消耗
        channel.basicConsume(RabbitmqConstant.QUEUE_TENCENT, false, new DefaultConsumer(channel) {
            /**
             * Delivery: 发送,递送
             *
             * @param consumerTag
             * @param envelope
             * @param properties
             * @param body
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("腾讯天气收到气象信息" + new String(body));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });

    }


}
